package com.api.core.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.api.core.exception.CodeBoxException;
import com.api.core.model.UserLoginModel;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class ReqUtil {

    public static String getUserId(HttpServletRequest request){
        UserLoginModel model = (UserLoginModel)request.getAttribute("userModel");
        return model.getOpenId();
    }

    public static int getRandom(int min, int max) {
        Random random = new Random();
        int int1 = random.nextInt(max) % (max - min + 1) + min;
        int int2 = random.nextInt(99) % (99 - 10 + 1) + 10;
        return int1 * int2;
    }

    public static void reqIsEmpty(String[] str, JSONObject req) {
        for (String s : str) {
            isEmpty(req.getString(s), s + "不能为空");
        }
    }
    public static void isEmpty(String value, String msg) {
        if (StringUtils.isEmpty(value))
            throw new CodeBoxException(msg);
    }


    public static Object getData(HttpServletRequest request, Class c) {
        try {
            return JSON.parseObject(request.getParameter("data"), c);
        } catch (Exception e) {
            throw new CodeBoxException("报文转换错误!");
        }
    }

    public static Map<String, Object> getDataToMap(HttpServletRequest request) {
        try {
            return JSON.parseObject(request.getParameter("data"));
        } catch (Exception e) {
            throw new CodeBoxException("报文转换错误!");
        }
    }

    public static Map<String, Object> objToMap(Object obj){
        return JSON.parseObject(JSON.toJSONString(obj));
    }



    public static JSONObject getData(HttpServletRequest request) {
        try {
            return JSON.parseObject(request.getParameter("data"));
        } catch (Exception e) {
            throw new CodeBoxException("报文转换错误!");
        }
    }

    public static List<?> getDataToList(HttpServletRequest request, Class c) {
        try {
            return JSON.parseArray(request.getParameter("data"), c);
        } catch (Exception e) {
            throw new CodeBoxException("报文转换错误!");
        }

    }



}
